﻿using Kinlo.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using XingLucifer.IBase.Enums;

namespace Kinlo.MESDocking
{
    /// <summary>
    /// 在制品进站校验接口
    /// </summary>
    public class WIPInStationCheck : MESInteractionBase
    {
        public WIPInStationCheck(Action<string, string, string, string, string, long, DateTime, string, string, string> log, Action<string, string, string> dialog)
            : base(log, dialog, "在制品进站校验接口")
        {

        }

        public override ResultTypes Interaction(string url, int timeout, int reconnect_count, DateTime dateTime, ProductionData[] production, Dictionary<string, object> parameters)
        {
            _stopwatch.Restart();
            try
            {
                string json = JsonSerializer.Serialize(parameters["parameter"], HttpHelper.Options);
                var result = Post(json, url, timeout, reconnect_count);
                _stopwatch.Stop();
                _log?.Invoke(production[0].BarCode, _name, result.Item1 ? result.Item2 : "请求异常", json, result.Item1 ? result.Item2 : "请求异常",
                    _stopwatch.ElapsedMilliseconds, dateTime, parameters["machineNo"].ToString(), parameters["producttype"].ToString(), "wIPInStationCheck");
                if (result.Item1)
                {
                    Dictionary<string, string> result_json = JsonSerializer.Deserialize<Dictionary<string, string>>(result.Item2, HttpHelper.Options);
                    if (result_json["Result"] != "OK")
                    {
                        //string error = string.Empty;
                        //switch (result_json["Return_Code"])
                        //{
                        //    case "E0001":
                        //        error = "上位机软件编号【上位机软件编号】校验失败";
                        //        break;
                        //    case "E0002":
                        //        error = "上位机【上位机软件编号】验证密码校验失败";
                        //        break;
                        //    case "E0003":
                        //        error = "设备编码【设备编码】校验失败";
                        //        break;
                        //    case "E0004":
                        //        error = "上传时间转换失败";
                        //        break;
                        //    case "E2401":
                        //        error = "在制品【在制品批次】不存在";
                        //        break;
                        //    case "E2402":
                        //        error = "在制品【在制品批次】未按照工艺路线进行生产";
                        //        break;
                        //    case "E2403":
                        //        error = "在制品【在制品批次】状态异常";
                        //        break;
                        //    case "E2404":
                        //        error = "在制品【在制品批次】已投入【累计投入次数】次，已超过重复投入次数【复投次数】";
                        //        break;
                        //    case "E2405":
                        //        error = "在制品【在制品批次】已被Marking【marking代码】拦截";
                        //        break;
                        //    case "E2406":
                        //        error = "在制品【在制品批次】流转时间已超期";
                        //        break;
                        //    case "E2407":
                        //        error = "在制品【在制品批次】工艺参数【工艺参数】异常";
                        //        break;
                        //    case "E2408":
                        //        error = "生产环境未达标，不允许生产，【温度超过标准3℃】（这里的错误描述需要定制）";
                        //        break;
                        //    case "E":
                        //        error = "未知异常";
                        //        break;
                        //}
                        _dialog?.Invoke(_name, "502", result_json["Message"]);
                        return ResultTypes.MES_NG;
                    }
                }
                else
                {
                    _dialog?.Invoke(_name, "ERROR", result.Item2);
                    return ResultTypes.MES上传失败;
                }
                return ResultTypes.合格;
            }
            catch (Exception ex)
            {
                _dialog?.Invoke(_name, "ERROR", $"异常:{ex}");
                return ResultTypes.MES上传失败;
            }
        }
    }
}
